<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
       
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
			
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
    <script src="lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script src="api/baseApi.js" charset="utf-8"></script>


<!--toolbar :头部工具栏-->
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
	<button class="layui-btn layui-btn-sm" lay-event="add">新增父菜单</button>
  </div>
</script>

<!-- 操作列 -->
<script type="text/html" id="auth-state">
	<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="addSon">新增子菜单</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<!--开关-->
<script type="text/html" id="switchTpl">
  <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|未启用" lay-filter="sexDemo" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script th:inline="none">
    layui.use(['table', 'treetable','form'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
		var form = layui.form;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            url: baseUrl + menuBaseUrl +'treeTableMenu',
			toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'menuname', minWidth: 200, title: '权限名称'},
                {field: 'permission', title: '权限标识'},
                {field: 'url', title: '菜单url'},
				{field: 'status', title: '状态',templet: '#switchTpl'},
                {field: 'orderNumber', width: 80, align: 'center', title: '排序号'},
                {
                    field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                        if (d.type == 2) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.type == 1) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim layui-bg-green">菜单</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 200, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            },
            text: {
                none: '暂无相关数据或权限不够'
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听右侧工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
				delMenu(data);
				obj.del();
				
            } else if (layEvent === 'edit') {
				editMenu(data);
				
            }else if(layEvent === 'addSon'){
				console.log(data);
				addMenu(data);
			}
        });
		
		//监听状态开关操作
        form.on('switch(sexDemo)', function(obj){
        
          var id = this.value; // 当行ID
          var status = this.checked ? '1' : '0'; // 选中状态
          var data ={id,status}
          $.ajax({
            url: baseUrl + menuBaseUrl ,
            type:'put',
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(data),
            success:function (resp) {
                console.log(resp)
              layer.msg(resp.msg)
            },
			error: function(resp){
				layer.msg(resp.msg)
			}

          })
        });
		
		//头工具栏事件
        table.on('toolbar(munu-table)', function(obj){
          var checkStatus = table.checkStatus(obj.config.id);
          switch(obj.event){
            case 'add':  
			  addMenu(obj.data);
              break;
          };
        });



        //添加子菜单
        function addMenu(data){
            //将数据存入本地sessionData中
            if(data){
                var obj ={'pid':data.id}
                layui.sessionData('menuAdd',{
                    key:'menu'
                    ,value:obj
                })
            }

            let index = layer.open({
                title:'添加菜单',
                type:2,
                shade:0.2,
                maxmin:true,
                shadeClose:true,
                area:['100%','100%'],
                content:'menuAdd',
                yes: function(index, layero){
                    parent.location.reload(); // 父页面刷新
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }
            })
        }

		//编辑菜单
		function editMenu(data){
			//将数据存入本地sessionData中
            layui.sessionData('menuData',{
              key:'menu'
              ,value:data
            })
            let index = layer.open({
              title:'编辑菜单',
              type:2,
              shade:0.2,
              maxmin:true,
              shadeClose:true,
              area:['100%','100%'],
              content:'menuEdit',
              yes: function(index, layero){
                parent.location.reload(); // 父页面刷新
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
              }
            })
		}
		
		//删除菜单
		function delMenu(data) {
            layer.confirm('真的删除么?', function (index) {
                $.ajax({
                    url: baseUrl + menuBaseUrl + data.id,
                    type: 'delete',
                    success: function (res) {
                        layer.msg(res.msg)
                        table.reload('munu-table')
                    },
                    error: function (res) {
                        layer.msg(res.msg)
                    }
                })
                layer.close(index);
            });
            table.reload('munu-table');
        }
		
	
			
    });
</script>
</body>
</html>